home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1997 January: Mac OS SDK / Dev.CD Jan 97 SDK2.toast / Development Kits (Disc 2) / OpenDoc / Developer Documentation / Recipes, Tech Notes & Articles / Tech Notes / Utilities Documentation / ODMath < prev    next >
Encoding:
Text File  |  1995-11-06  |  3.8 KB  |  58 lines  |  [TEXT/ttxt]

  1. OpenDoc™ Utilities Documentation
  2.  
  3. ODMath: Fixed-Point Math Support
  4. 10 October 1995
  5.  
  6. © 1993-1995  Apple Computer, Inc. All Rights Reserved.
  7. Apple, the Apple logo, and Macintosh are registered trademarks of Apple Computer, Inc.
  8. Mac and OpenDoc are trademarks of Apple Computer, Inc.
  9.  
  10.  
  11. What It Does
  12.  
  13. ODMath is a set of utilities for working with the the OpenDoc numeric types ODFixed (aka ODCoordinate) and ODFract. These are both 32-bit fixed-point numbers. These types both represent real numbers that have been scaled up and rounded to integers. ODFixed has been scaled up by a factor of 2^16 (65,536) while ODFract has been scaled up by 2^30 (1,073,741,824). Another way to look at it is that an ODFixed has an implicit “binary point” between the upper and lower word, while an ODFract has the binary point two bits from the most significant bit.
  14. This gives an ODFixed a range of -32768.0 to nearly +32768, with a resolution of 1/32,768 (about 3x10^-5). ODFract has a range of only -2 ... +2, but with a resolution of 1/1,073,741,824 (about 10^-9).
  15.  
  16. In addition, there is some support for the type ODWide, which is a 64-bit integer. ODWide is not used directly in the OpenDoc API, but is useful for storing intermediate results in computations on integers, ODFixeds and ODFracts. The utility LineOps.cpp has examples of this.
  17.  
  18. Constants
  19.  
  20. Several common values are available as named constants:
  21. kODFixed1
  22. kODFixedHalf
  23. kODFract11
  24. kODFixedInfinity — the largest possible positive ODFixed value
  25. kODFixedMinusInfinity — the largest possible negative ODFixed value
  26.  
  27. Conversion Functions
  28.  
  29. ODFixedRound converts an ODFixed to an integer, rounding toward the nearest integer.
  30. ODIntToFixed converts an integer to an ODFixed.
  31. ODFixedToFract converts an ODFixed to an ODFract.
  32. ODFractToFixed converts an ODFract to an ODFixed.
  33. ODFixedToFloat converts an ODFixed to a floating point number.
  34. ODFloatToFixed converts a floating point number to ODFixed.
  35.  
  36. All of these functions are implemented inline as simple shifts, multiplies and divides. They do not perform any range checking and will return bogus values if the input value is not in the correct range.
  37.  
  38. There are some missing functions here, such as ODFractToFloat and ODFloatToFract. They're left as exercises for the reader, and are easy to figure out if you look at the definitions of the existing conversions.
  39.  
  40. Arithmetic
  41.  
  42. Fixed point numbers can be added and subtracted like integers, so you don't need any special functions for addition or subtraction. You can also multiply/divide an ODFixed or ODFract with an integer by using regular integer multiplication or division.
  43.  
  44. ODFixedMultiply, ODFixedDivide multiply and divide ODFixeds. They return the appropriate “infinity” value (see the Constants section above) in the case of overflow.
  45. ODFractMultiply, ODFractDivide multiply and divide ODFracts, with similar overflow behavior.
  46. ODFractSinCos computes the sine and cosine of an angle. The angle is an ODFixed whose units are radians, and the results are ODFracts.
  47.  
  48. ODWide Operations
  49.  
  50. ODWideCompare compares two ODWide values for equality.
  51. ODWideIsLong determines whether an ODWide value is within the range of a long integer. (If so, the integer value is in the “lo” field of the ODWide struct.)
  52. ODWideShift shifts the bits of a wide integer. A positive shift makes the number larger, a negative shift makes it smaller.
  53. ODWideNegate flips the sign of an ODWide value (subtracts it from zero.)
  54. ODWideAdd, ODWideSubtract operate on two ODWide values.
  55. ODWideMultiply multiplies two 32-bit integers and produces an ODWide result.
  56. ODWideDivide divides an ODWide by a 32-bit integer. It has a third pointer parameter in which the remainder is returned. If you don't care about the remainder, pass kODIgnoreRemainder in the 'remainder' parameter.
  57. ODWideSquareRoot takes the square root of an ODWide, returning an integer.
  58.